package com.lazyshopkeeper.front.apiService.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lazyshopkeeper.front.apiService.IItemService;
import com.lazyshopkeeper.front.common.Constant;
import com.taobao.api.ApiException;
import com.taobao.api.TaobaoClient;
import com.taobao.api.domain.Item;
import com.taobao.api.request.ItemGetRequest;
import com.taobao.api.request.ItemUpdateRequest;
import com.taobao.api.request.ItemsInventoryGetRequest;
import com.taobao.api.request.ItemsOnsaleGetRequest;
import com.taobao.api.response.ItemGetResponse;
import com.taobao.api.response.ItemUpdateResponse;
import com.taobao.api.response.ItemsInventoryGetResponse;
import com.taobao.api.response.ItemsOnsaleGetResponse;

@Service
public class ItemService implements IItemService{

	@Autowired
	private TaobaoClient client;

	@Override
	public Item getItem(long num_iid, String sessionKey) throws ApiException {
		ItemGetRequest igReq = new ItemGetRequest();
		igReq.setNumIid(num_iid);
		igReq.setFields(Constant.FILDS_ITEM);
		ItemGetResponse igRes = client.execute(igReq, sessionKey);
		
		return igRes.getItem();
	}

	@Override
	public List<Item> getItemsAll(String sortType,String sessionKey) throws ApiException {
		List<Item> items = new ArrayList<Item>();
		items.addAll(this.getItemsforOnsale(sortType,sessionKey));
		items.addAll(this.getItemsforInventory(sortType,sessionKey));
		return items;
	}

	@Override
	public List<Item> getItemsforOnsale(String sortType,String sessionKey) throws ApiException {
		List<Item> items = new ArrayList<Item>();
		for(long i=1;i<=Constant.PAGE_NO_MAX;i++){
			// 调用api
			ItemsOnsaleGetRequest req=new ItemsOnsaleGetRequest();
			req.setFields(Constant.FILDS_ITEM_LIST);
			req.setPageNo(i);
			req.setPageSize(Constant.PAGE_SIZE_MAX);
			
			req.setOrderBy(sortType);
			ItemsOnsaleGetResponse response = client.execute(req , sessionKey);
			if(response.getItems()==null||response.getItems().size()==0)
				break;
			items.addAll(response.getItems());
		} 
		return items;
	}

	@Override
	public List<Item> getItemsforInventory(String sortType,String sessionKey)
			throws ApiException {
		List<Item> items = new ArrayList<Item>();
		for(long i=1;i<=Constant.PAGE_NO_MAX;i++){
			// 调用api
			ItemsInventoryGetRequest req=new ItemsInventoryGetRequest();
			req.setFields(Constant.FILDS_ITEM_LIST);
			req.setPageNo(i);
			req.setPageSize(Constant.PAGE_SIZE_MAX);
			req.setOrderBy(sortType);
			ItemsInventoryGetResponse response = client.execute(req , sessionKey);
			if(response.getItems()==null||response.getItems().size()==0)
				break;
			items.addAll(response.getItems());
		} 
		return items;
	}

	@Override
	public Item updateItemByDesc(Item item,String sessionKey)
			throws ApiException {
			Item items=new Item();
				ItemUpdateRequest req=new ItemUpdateRequest();
				req.setNumIid(item.getNumIid());
				req.setDesc(item.getDesc());
				ItemUpdateResponse response=client.execute(req,sessionKey);	
				if(response.getItem()!=null)
					items=response.getItem();
		return items;
	}

}
